import { requestLoading, request } from '../../utils/network.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, // 当前页码
    pageSize: 20, // 每页数的数据大小
    hasMoreData: false, // 是否有更多的数据
    orderlist:[
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "2",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "3",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "4",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "5",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // }
    ],
    showlist: [], // 收起展开的列表
    screen:true,  // 是否隐藏条件查询view
    merselect: '全部商户',
    _merId: '',// 商户的选中值
    shoplist:[
      // {
      //   supplierId: 2,
      //   shopName: "大刀公司"
      // },
      // {
      //   supplierId: 3,
      //   shopName: "小牛公司"
      // },
      // {
      //   supplierId: 4,
      //   shopName: "小大公司"
      // },
      // {
      //   supplierId: 5,
      //   shopName: "小牛大司"
      // }
    ] // 商户列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let orderlist = that.data.orderlist;
    let showlist = that.data.showlist;
    that.fetchBillList('正在加载') 
    that.fetchMyshop() 
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function () {
      let that = this
      that.fetchBillList('正在刷新数据') 
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function (){
      if (this.data.hasMoreData) {
        this.getMusicInfo('加载更多数据')
      } else {
        wx.showToast({
          title: '没有更多数据',
          icon: 'none'
        })
      }
    },
  // 收起或展开 地址信息
  showAddr:function(e){
    let that = this;
    let index = e.target.dataset.index
    let show = "showlist[" + index + "]";
    let value = that.data.showlist[index]
    that.setData({
      [show]: !value
    })
  },
  // 条件筛选
  changeStatus: function(e){
    let that = this;
    that.setData({
      screen: !that.data.screen
    })
  },

  // 点击条件
  screenItem:function (e){
    let id = e.target.dataset.id
    let name = e.target.dataset.name
    let that = this;
    // merchant 商户条件
    that.setData({
      merselect: name,
      _merId: id,
      screen: true
    })

    // 根据查询条件
    // that.fetchMyshop()
    that.fetchBillList('请求数据...')
  },
  // 取货结果
  receiveStatus(e) {
    let that = this
    let orderid = e.currentTarget.dataset.orderid
    let status = e.currentTarget.dataset.status
    if (status === '3') {
      wx.showModal({
        title: '提示',
        content: '您确定该商品在当前供应商处取货失败',
        success: function (res) {
          if (res.confirm) {
            that.fetchReceiveStatus(orderid, '3')
          } else if (res.cancel) {
          }
        }
      })
    } else {
      that.fetchReceiveStatus(orderid, status)
    }
  },
  // 取货请求
  fetchReceiveStatus (orderid, status) {
    let data ={
      distributionBillId: orderid,
      status: status
    }
    request(
      '/distributor/updateBillStatus',
      data,
      function success(res) {
        if (status === '2' && res.resultCode === 2000) {
          wx.showToast({
            title: '成功获取',
            icon: 'success',
            duration: 1000
          })
        }
        if (status === '3' && res.resultCode === 2000) {
          wx.showToast({
            title: '确认认领状态',
            icon: 'none',
            duration: 1000
          })
        }
      },
      function fail (){
        wx.showToast({
          title: '网络连接失败',
          icon: 'none',
          duration: 1000
        })
      }
    )
  },
  /**
   * 获取配货单
   */
  fetchBillList (message){
    let that = this
    let showlist =[]
    let data = {
      distributionStatus: 1,
      page: that.data.page,
      number: that.data.pageSize,
      supplierId: that.data._merId
    }
    requestLoading(
      '/distributor/myBillList',
      data,
      message,
      function success(res) {
        var orderlist = that.data.orderlist
        if (res.resultCode === 2000) {
          if (that.data.page === 1) {
            orderlist = []
          }
          var list = res.data.list
          if (list.length < that.data.pageSize) {
            that.setData({
              orderlist: orderlist.concat(list),
              hasMoreData: false
            })
          } else {
            that.setData({
              orderlist: orderlist.concat(list),
              hasMoreData: true,
              page: that.data.page + 1
            })
          }
          // showlist的数组
          that.data.orderlist.map((item, index) => {
            showlist.push(false)
          })
          that.setData({
            showlist: showlist
          })
        }else {
          wx.showToast({
            title: res.resultMsg,
            icon: 'none'
          })
        }
      },
      function fail() {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none',
          duration: 500
        })
      }
    )
  },
  /**
   * 获取我的商户
   */
  fetchMyshop (){
    let that = this
    request(
      '/distributor/myShop',
      '',
      function success (res){
        if (res.resultCode === 2000) {
          that.setData({
            shoplist: res.data
          })
        } 
      },
      function fail (){
        wx.showToast({
          title: '加载数据失败',
          icon: 'none'
        })
      }
    )
  }
})